<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <div id="root1"></div>
    <div id="root2"></div>
    <div id="root3"></div>

    <script src="https://unpkg.zhimg.com/react@17.0.2/umd/react.development.js"></script>
    <script src="https://unpkg.zhimg.com/react-dom@17.0.2/umd/react-dom.development.js"></script>
    <script src="https://unpkg.zhimg.com/@babel/standalone/babel.min.js"></script>
    <script src="https://unpkg.zhimg.com/prop-types@15.7.2/prop-types.js"></script>
    <script type="text/babel">
        class Person extends React.Component {
            
            // 类型规则
            static propTypes = {
                name: PropTypes.string.isRequired,
                age: PropTypes.number,
                sex: PropTypes.string,
                say: PropTypes.func
            }

            // 默认值
            static defaultProps = {
                sex: '保密'
            }

            // 构造器是否接收props且是否传递给super，主要影响constructor内使用this方式props
            constructor(props) {
                super(props)

                console.log(this.props) // 如果不传递给super，此处undefined
            }

            render() {
                const {name, age, sex} = this.props

                return (
                    <ul>
                        <li>姓名：{name}</li>
                        <li>年龄：{age}</li>
                        <li>性别：{sex}</li>
                    </ul>
                )
            }
        }
       
        ReactDOM.render(<Person name="JS" age="18" sex="男" />, document.getElementById('root1'))
        ReactDOM.render(<Person name="Vue" age="18" sex="男" />, document.getElementById('root2'))

        // props批量传递
        const p = {
            name: 'React',
            age: 19,
            sex: '女',
            say() {
                console.log('say...')
            }
        }
        ReactDOM.render(<Person {...p} />, document.getElementById('root3'))
    </script>
</body>
</html>